package com.lollipop.designpattern.strategy;

import org.jetbrains.annotations.Nullable;

/**
 * @Author: J.K
 * @Date: 2023-03-27 14:07
 * @Description: 枚举策略模式，根据不同类型保存到相应的库中
 */
public enum StrategyEnum {
    /**
     * 数据库
     */
    DB("db") {
        @Override
        public void save(Object obj) {
            System.out.printf("save in db: %s", obj);
        }
    },
    /**
     * 文件
     */
    FILE("file") {
        @Override
        public void save(Object obj) {
            System.out.println("save in file: " + obj);
        }
    },
    /**
     * OSS
     */
    OSS("oss") {
        @Override
        public void save(Object obj) {
            System.out.println("save in oss: " + obj);
        }
    };

    private String type;

    StrategyEnum(String type) {
        this.type = type;
    }

    @Nullable
    public static StrategyEnum typeOf(String type) {
        for (StrategyEnum strategyEnum : values()) {
            if (strategyEnum.type.equals(type)) {
                return strategyEnum;
            }
        }
        return null;
    }

    /**
     * 保存数据
     *
     * @param obj
     */
    public abstract void save(Object obj);
}
